package cn.iocoder.yudao.module.erp.service.payinfo;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.erp.dal.dataobject.customeraccount.CustomerAccountDO;
import cn.iocoder.yudao.module.erp.dal.mysql.customeraccount.CustomerAccountMapper;
import cn.iocoder.yudao.module.erp.enums.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import cn.iocoder.yudao.module.erp.controller.admin.payinfo.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.payinfo.PayInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.payinfo.PayInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 付款记录信息 Service 实现类
 *
 * @author mhc
 */
@Service
@Validated
public class PayInfoServiceImpl implements PayInfoService {

    @Resource
    private PayInfoMapper payInfoMapper;

    @Resource
    private CustomerAccountMapper customerAccountMapper;

    @Autowired
    private ConcurrentHashMap<String, CustomerAccountDO> concurrentHashMap;

    @Override
    @Transactional
    public Long createPayInfo(PayInfoSaveReqVO createReqVO) {
        // 插入
        PayInfoDO payInfo = BeanUtils.toBean(createReqVO, PayInfoDO.class);
        payInfo.setCompanyId(SecurityFrameworkUtils.getLoginUserDeptId());


        BigDecimal newPayCount=payInfo.getPayCount();
        //计算金额

        //得到记录
        //CustomerAccountDO customerAccountDO=customerAccountMapper.selectOne(CustomerAccountDO::getCustomerId,payInfo.getCustomerId());

        if(payInfo.getType().equals("3")){
          //我方收款
            CustomerAccountDO salecustomer=new CustomerAccountDO();
            salecustomer.setCustomerId(payInfo.getCustomerId());
            salecustomer.setNowCount(newPayCount);
            salecustomer.setSaleCount(new BigDecimal("0"));
            salecustomer.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer);

            payInfo.setPayCount(payInfo.getPayCount().abs());

        }else if(payInfo.getType().equals("4")){
            //我方付款
            //customerAccountDO.setNowCount(customerAccountDO.getNowCount().subtract(payInfo.getPayCount()));

            CustomerAccountDO salecustomer=new CustomerAccountDO();
            salecustomer.setCustomerId(payInfo.getCustomerId());
            salecustomer.setNowCount(newPayCount.negate());
            salecustomer.setSaleCount(new BigDecimal("0"));
            salecustomer.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer);

            payInfo.setPayCount(payInfo.getPayCount().negate());
        }else{
            throw exception(COMMON_EXCEPTION);
        }
       // customerAccountMapper.updateById(customerAccountDO);


        payInfoMapper.insert(payInfo);

        // 返回
        return payInfo.getId();
    }

    @Override
    @Transactional
    public void updatePayInfo(PayInfoSaveReqVO updateReqVO) {
        // 校验存在
        //validatePayInfoExists(updateReqVO.getId());
        PayInfoDO old=payInfoMapper.selectById(updateReqVO.getId());

        if (old == null) {
            throw exception(PAY_INFO_NOT_EXISTS);
        }

        BigDecimal oldPayCount=  old.getPayCount();
        BigDecimal newPayCount=updateReqVO.getPayCount();
        // 更新




        //得到记录
       // CustomerAccountDO customerAccountDO=customerAccountMapper.selectOne(CustomerAccountDO::getCustomerId,old.getCustomerId());



        if(updateReqVO.getType().equals("3")){//我方收款
           // customerAccountDO.setNowCount(customerAccountDO.getNowCount().subtract(oldCount).add(updateObj.getPayCount()));

            CustomerAccountDO salecustomer=new CustomerAccountDO();
            salecustomer.setCustomerId(old.getCustomerId());
            salecustomer.setNowCount(oldPayCount.negate());
            salecustomer.setSaleCount(new BigDecimal("0"));
            salecustomer.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer);



            CustomerAccountDO salecustomer2=new CustomerAccountDO();
            salecustomer2.setCustomerId(updateReqVO.getCustomerId());
            salecustomer2.setNowCount(newPayCount);
            salecustomer2.setSaleCount(new BigDecimal("0"));
            salecustomer2.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer2);



        }else if(updateReqVO.getType().equals("4")){//我方付款
           // customerAccountDO.setNowCount(customerAccountDO.getNowCount().add(oldCount).subtract(updateObj.getPayCount()));


            CustomerAccountDO salecustomer=new CustomerAccountDO();
            salecustomer.setCustomerId(old.getCustomerId());
            salecustomer.setNowCount(oldPayCount);
            salecustomer.setSaleCount(new BigDecimal("0"));
            salecustomer.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer);


            CustomerAccountDO salecustomer2=new CustomerAccountDO();
            salecustomer2.setCustomerId(updateReqVO.getCustomerId());
            salecustomer2.setNowCount(newPayCount.negate());
            salecustomer2.setSaleCount(new BigDecimal("0"));
            salecustomer2.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer2);

            updateReqVO.setPayCount(updateReqVO.getPayCount().negate());
        }else{
            throw exception(COMMON_EXCEPTION);
        }
      //  customerAccountMapper.updateById(customerAccountDO);


        PayInfoDO updateObj = BeanUtils.toBean(updateReqVO, PayInfoDO.class);
        payInfoMapper.updateById(updateObj);

    }

    @Override
    @Transactional
    public void deletePayInfo(Long id) {
        // 校验存在
       // validatePayInfoExists(id);
        PayInfoDO old=payInfoMapper.selectById(id);
        if (old == null) {
            throw exception(PAY_INFO_NOT_EXISTS);
        }

        //得到记录
        //CustomerAccountDO customerAccountDO=customerAccountMapper.selectOne(CustomerAccountDO::getCustomerId,old.getCustomerId());


        if(old.getType().equals("3")){//我方收款
            //customerAccountDO.setNowCount(customerAccountDO.getNowCount().subtract(old.getPayCount()));
            CustomerAccountDO salecustomer=new CustomerAccountDO();
            salecustomer.setCustomerId(old.getCustomerId());
            salecustomer.setNowCount(old.getPayCount().negate());
            salecustomer.setSaleCount(new BigDecimal("0"));
            salecustomer.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer);



        }else if(old.getType().equals("4")){//我方付款
            //customerAccountDO.setNowCount(customerAccountDO.getNowCount().add(old.getPayCount()));

            CustomerAccountDO salecustomer=new CustomerAccountDO();
            salecustomer.setCustomerId(old.getCustomerId());
            salecustomer.setNowCount(old.getPayCount());
            salecustomer.setSaleCount(new BigDecimal("0"));
            salecustomer.setBuyCount(new BigDecimal("0"));
            concurrentHashMap.put(UUID.randomUUID().toString().replace("-", ""),salecustomer);

        }else{
            throw exception(COMMON_EXCEPTION);
        }
      //  customerAccountMapper.updateById(customerAccountDO);

        // 删除
        payInfoMapper.deleteById(id);
    }

    private void validatePayInfoExists(Long id) {
        if (payInfoMapper.selectById(id) == null) {
            throw exception(PAY_INFO_NOT_EXISTS);
        }
    }

    @Override
    public PayInfoDO getPayInfo(Long id) {
        return payInfoMapper.selectById(id);
    }

    @Override
    public PageResult<PayInfoDO> getPayInfoPage(PayInfoPageReqVO pageReqVO) {
        return payInfoMapper.selectPage(pageReqVO);
    }

}